import {
  getTempCheckTable,
  editTempCheckRecord
} from '../../api/operate'
import {
  TEMP_CHECK_LIST,
  TEMP_CHECK_CURRENT_ROW,
  TEMP_CHECK_DATA_COUNT,
  TEMP_CHECK_HISTORY_DATA,
  TEMP_CHECK_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    tempCheckList: [],
    tempCheckDataCount: null,
    tempCheckPageSize: 20,
    tempCheckHistoryData: [],
    row: {
      tag: '',
      content: '',
      state: '',
      ctime: '',
      des: '',
      company: ''
    }
  },
  getters: {
    tempCheckList: state => state.tempCheckList
  },
  mutations: {
    [TEMP_CHECK_LIST] (state, data) {
      state.tempCheckList = data
    },
    [TEMP_CHECK_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [TEMP_CHECK_DATA_COUNT] (state, data) {
      state.tempCheckDataCount = data
    },
    [TEMP_CHECK_HISTORY_DATA] (state, data) {
      state.tempCheckHistoryData = data
    },
    [TEMP_CHECK_PAGE_SIZE] (state, data) {
      state.tempCheckPageSize = data
    }
  },
  actions: {
    async getTempCheckList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getTempCheckTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          tag: item.tag,
          content: item.content,
          state: item.state.toString(),
          ctime: item.ctime,
          des: item.des,
          company: item.company
        }]
      })
      commit(TEMP_CHECK_LIST, newData)
      commit(TEMP_CHECK_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.templateCheck.tempCheckPageSize) {
        commit(TEMP_CHECK_HISTORY_DATA, newData)
      } else {
        commit(TEMP_CHECK_HISTORY_DATA, newData.slice(0, rootState.templateCheck.tempCheckPageSize))
      }
      return resp
    },
    async editTempCheckData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editTempCheckRecord(data)
      return resp
    },
    async getTempCheckCurrentRow ({ commit, rootState }, row) {
      commit(TEMP_CHECK_CURRENT_ROW, row)
    },
    async changeTempCheckPage ({ commit, rootState }, index) {
      let data = rootState.templateCheck.tempCheckList
      let _start = (index - 1) * rootState.templateCheck.tempCheckPageSize
      let _end = index * rootState.templateCheck.tempCheckPageSize
      commit(TEMP_CHECK_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeFundingPageSize ({ commit, rootState }, value) {
      commit(TEMP_CHECK_PAGE_SIZE, value)
    }
  }
}
